package com.ylbh.modules.sign.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylbh.common.exception.NormalException;
import com.ylbh.common.pojo.activity.dto.ActivityGoodsDTO;
import com.ylbh.common.pojo.sign.dto.SignUserLogDTO;
import com.ylbh.common.pojo.sign.vo.ShoppingSigninUserlogVO;
import com.ylbh.common.pojo.sign.vo.UserlogExceVO;
import com.ylbh.common.utils.*;
import com.ylbh.modules.activity.mapper.ActivityGoodsMapper;
import com.ylbh.modules.shoppingcoupon.entity.ShoppingCoupon;
import com.ylbh.modules.shoppingcoupon.mapper.ShoppingCouponMapper;
import com.ylbh.modules.sign.entity.ShoppingSigninIssuelog;
import com.ylbh.modules.sign.mapper.ShoppingSignUserlogMapper;
import com.ylbh.modules.sign.service.ShoppingSigninUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ShoppingSigninUserServiceImpl implements ShoppingSigninUserService {

    @Autowired
    private ShoppingSignUserlogMapper shoppingSignUserlogMapper;
    @Autowired
    private ShoppingCouponMapper shoppingCouponMapper;
    @Autowired
    private ActivityGoodsMapper activityGoodsMapper;
    @Value("${url.prizeUrl}")
    private String prizeUrl;

    @Override
    public PageUtils queryUserLogList(SignUserLogDTO signUserLogDTO) {
        Page<Object> page = new Page<>(signUserLogDTO.getPage(),signUserLogDTO.getLimit());
        List<ShoppingSigninUserlogVO> list = shoppingSignUserlogMapper.selectUserLogList(signUserLogDTO,page);
        for (ShoppingSigninUserlogVO vo : list) {
            if (vo.getRewardType() == "2") {
                ShoppingCoupon shoppingCoupon = shoppingCouponMapper.selectCouponInfoById(vo.getRewardItem());
                vo.setReleaseType(shoppingCoupon.getReleaseType());
                vo.setTitle(shoppingCoupon.getTitle());
            } else if (vo.getRewardType() == "3") {
                ActivityGoodsDTO activityGoodsDTO = activityGoodsMapper.getActivityGoodsById(Integer.parseInt(vo.getRewardItem()));
                vo.setGoodsName(activityGoodsDTO.getGoodsName());
                vo.setImg(activityGoodsDTO.getImg());
            }
        }
        Page<ShoppingSigninUserlogVO> page1 = new Page<>();
        page1.setRecords(list).setTotal(page.getTotal());
        return new PageUtils(page1);
    }

    @Override
    public void addIssueLogInfo(ShoppingSigninIssuelog shoppingSigninIssuelog) {
        IdWorker idW = new IdWorker();
        shoppingSigninIssuelog.setId(idW.nextId());
        shoppingSigninIssuelog.setCreateTime(new Date());
        shoppingSigninIssuelog.setCreateUserId(ShiroUtils.getUserId().toString());

        int index = shoppingSignUserlogMapper.insertIssueLogInfo(shoppingSigninIssuelog);
        if (!StringUtils.isEmpty(index) && shoppingSigninIssuelog.getUserlogId() != null) {
            System.out.println(shoppingSigninIssuelog.getUserlogId());
            Map<String,String> map = new HashMap<>();
            map.put("addressInfo",shoppingSigninIssuelog.getContactAddress());
            map.put("expresssName",shoppingSigninIssuelog.getCompanyName());
            map.put("expressNo",shoppingSigninIssuelog.getLogisticsNumber());
            map.put("orderNo",shoppingSigninIssuelog.getId());
            map.put("sendTime",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            map.put("userId",shoppingSigninIssuelog.getUserlogId());
            String str = HttpClientHelperUtils.sendHttpPosts(prizeUrl,map);
            System.out.println(str);
            shoppingSignUserlogMapper.updateRewardStatus(shoppingSigninIssuelog.getUserlogId());
        }
    }

    @Override
    public void userlogExcel(HttpServletRequest request, HttpServletResponse response) throws NormalException {
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        List<UserlogExceVO> list = shoppingSignUserlogMapper.selectUserLogExceList(startTime, endTime);
        for (UserlogExceVO  vo : list) {
            if (vo.getRewardType() == "2") {
                ShoppingCoupon shoppingCoupon = shoppingCouponMapper.selectCouponInfoById(vo.getRewardItem());
                vo.setReleaseType(shoppingCoupon.getReleaseType());
                vo.setTitle(shoppingCoupon.getTitle());
            } else if (vo.getRewardType() == "3") {
                ActivityGoodsDTO activityGoodsDTO = activityGoodsMapper.getActivityGoodsById(Integer.parseInt(vo.getRewardItem()));
                vo.setGoodsName(activityGoodsDTO.getGoodsName());
            }
        }
        ExcelUtils.exportExcel(list, "抽奖记录","",UserlogExceVO.class, System.currentTimeMillis() + ".xls", response);
    }
}
